# Jordan Tam, PhD Thesis research
# University of British Columbia, CA
# Prepared by Tim Waring
# for publication
# 2019.11.04


# Hierarchical partitioning analysis

library(hier.part)

mydata<-read.csv(file.choose())
# Select file "community data.csv"

# mydata<-mydata[-c(8:9),]

##Define the vector of predictors
xcan<-mydata[,c("Bm_inco_perc", "Ia_risk",  "Hf_trust_gen", "Ga_QoL", "Hj_conx_teachers", "Kj_disagree", "Kf_poach")] #"soc_rel_index",



##Run hierarchical partitioning
woot<-hier.part(mydata$game_cost, xcan, family = "gaussian", gof = "Rsqu", barplot = TRUE)
woot
####Randomization Test for Hierarchical Partitioning
rand.hp(mydata$CCM_avg, xcan, fam = "gaussian", gof = "Rsqu", num.reps=1000)$Iprobs

##################Instead of barchart give output of all models, labeled

gofs <- all.regs(mydata$game_cost, xcan, family = "gaussian", gof = "Rsqu", print.vars = TRUE)

partition(gofs, pcan = 8, var.names = names(xcan))

